#include<bits/stdc++.h>
using namespace std;


const int MOD=1e9+7;
class Solution {
public:
    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
        priority_queue<pair<int,int>,vector<pair<int,int>>,less<pair<int,int>> >  buyq; //价格,数量 最大堆
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>  sellq; //价格,数量 最小堆
        int len=orders.size();
        for(auto v:orders){
            if(v[2]==0){
                if(sellq.empty()||sellq.top().first>v[0]){
                    buyq.emplace(v[0],v[1]);
                    continue;
                }
                while(v[1]>0&&!sellq.empty()&&sellq.top().first <= v[0]){
                    auto [sp,sm]=sellq.top();sellq.pop();
                    if(sm>v[1]){
                        sm-=v[1];
                        v[1]=0;
                        sellq.emplace(sp,sm);
                        break;
                    }else if(v[1]>sm){
                        v[1]-=sm;
                    }else{
                        v[1]=0;
                        break;
                    }
                }
                if(v[1]!=0){
                    buyq.emplace(v[0],v[1]);
                }
            }else if(1==v[2]){
                if(buyq.empty()||buyq.top().first<v[0]){
                    sellq.emplace(v[0],v[1]);
                    continue;
                }
                while(v[1]>0 && !buyq.empty() &&buyq.top().first >= v[0]){
                    auto [sp,sm]=buyq.top();buyq.pop();
                    if(sm>v[1]){
                        sm-=v[1];
                        v[1]=0;
                        buyq.emplace(sp,sm);
                        break;
                    }else if(v[1]>sm){
                        v[1]-=sm;
                    }else{
                        v[1]=0;
                        break;
                    }
                }
                if(v[1]!=0){
                    sellq.emplace(v[0],v[1]);
                }
            }
        }
        int ans=0;
        while(!buyq.empty()){
            auto [sp,sm]=buyq.top();buyq.pop();
            ans=(ans+sm)%MOD;
        }
        while(!sellq.empty()){
            auto [sp,sm]=sellq.top();sellq.pop();
            ans=(ans+sm)%MOD;
        }
        return ans;
    }
};

int main(){

    Solution ss;
    vector<vector<int>> v={
        {10,5,0},
        {15,2,1},
        {25,1,1},
        {30,4,0}
    };
    cout << ss.getNumberOfBacklogOrders(v) <<endl;


    return 0;
}